package de.homer.persistence.utils;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

/**
 * 
 * Helper Class for creating {@link SessionFactory}s.
 * 
 * @author André Stannek
 * 
 */
public final class HibernateSessionFactory {

	/**
	 * the instance if the session factory.
	 */
	private static final SessionFactory SESSION_FACTORY;

	static {
		try {
			// Create the SessionFactory from hibernate.cfg.xml
			SESSION_FACTORY = new AnnotationConfiguration().configure()
					.buildSessionFactory();
		} catch (Throwable ex) {
			// Make sure you log the exception, as it might be swallowed
			System.err.println("Initial SessionFactory creation failed." + ex);
			throw new ExceptionInInitializerError(ex);
		}
	}

	/**
	 * Get a Hibernate {@link SessionFactory}.
	 * 
	 * @return the singleton instance of hibernate session factory.
	 */
	public static SessionFactory getSessionFactory() {
		return SESSION_FACTORY;
	}
	
	/**
	 * Masking utility class constructor.
	 */
	private HibernateSessionFactory() {
		super();
	}
}
